package com.jmu.medicineknows.config;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

/**
 * @author Administrator
 */
@Configuration
public class WebMvcConfig implements WebMvcConfigurer {

    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry
                .addMapping("/**")
                .allowCredentials(true)
                .allowedOriginPatterns("*")
                .allowedMethods("*")
                .allowedHeaders("*")
                .maxAge(3600);
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        /**
         * @author ygc
         * 动态获取项目路径
         *
         * 因为部署项目后可能放在不同磁盘所以改为动态路径配置
         */
        //判断操作系统
        String os = System.getProperty("os.name");
        if (os.toLowerCase().startsWith("win")) {  //如果是Windows系统
            //项目相对路径+项目动态绝对路径
            registry.addResourceHandler("/uploads/**").
                    addResourceLocations("file:" + "D:\\uploads\\");
        } else {//linux和mac系统
            registry.addResourceHandler("/uploads/**").
                    addResourceLocations("file:" + "/root/uploads/");
        }
    }
}
